<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/14
 * Time: 16:55
 */

namespace app\index\model;

use think\Db;
use think\Exception;

class Activity
{
    public function formatActivityID($id_arr)
    {
        $tmp_arr = [];
        foreach ($id_arr as $id) {
            if ($id) $tmp_arr[] = $id;
        }
        if (empty($tmp_arr)) return [];

        $act_arr = [];
        foreach ($tmp_arr as $id) {
            $act_arr[substr($id, 0, 1)][] = substr($id, 1);
        }
        return $act_arr;
    }

    public function deleteActivity($id_arr)
    {
        $id_arr = $this->formatActivityID($id_arr);
        print_r($id_arr);
        if ($id_arr['P']) {
            try {
                $this->deleteBuyGifts($id_arr['P']);
            } catch (Exception $e) {
                throw  new Exception($e->getMessage());
            }
        }
    }

    public function addActivity($id_arr)
    {
        $id_arr = $this->formatActivityID($id_arr);
        print_r($id_arr);
        if ($id_arr['P']) {
            try {
                $this->addBuyGifts($id_arr['P']);
            } catch (Exception $e) {
                throw new Exception($e->getMessage());
            }
        }
    }

    ///////////////////////买赠活动/////////////////////////

    public function getGiftsInfo($id_arr, $field)
    {
        $map['activity.id'] = ['in', $id_arr];
        $field = $field ?: [
            'gift.id',
            'gfit.`code` product_id',
            'gift.`name` product_name',
            'category_class1 product_class1',
            'category_class2 product_class2',
            'category_class3 product_class3',
            'brief_description product_briefDescription',
            'unit product_unit',
            'number product_number',
            'thumb_image product_headImage',
            'real_weight',
            'stock product_stock',
            '1 is_gift'
        ];
        return DB::table('homegarden_activity.buy_gifts')->alias('activity')
            ->join('homegarden_cms.product gift', 'gift.id = activity.gift_id', 'LEFT')
            ->where($map)->column(implode(', ', $field));
    }

    public function addBuyGifts($id_arr)
    {
        $map['activity.id'] = ['in', $id_arr];
        $data = DB::table('homegarden_activity.buy_gifts')->alias('activity')
            ->join('homegarden_cms.product gift', 'gift.id = activity.gift_id', 'LEFT')
            ->where($map)
            ->column("CONCAT('P', id) active_id, gift.`code` product_id, activity.buy, activity.gift");

        $gifts = $this->getGiftsInfo($id_arr);
        try {
            DB::table('homegarden.present')->insertAll($data);
            DB::table('homegarden.product')->insertAll($gifts);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }

    public function deleteBuyGifts($id_arr)
    {
        $id_arr = array_map("getPresentID", $id_arr);
        $map['active_id'] = ['in', $id_arr];
        $gifts_id = $this->getGiftsInfo($id_arr, ['gift.id']);
        $where['id'] = ['in', $gifts_id];
        try {
            DB::table('homegarden.present')->where($map)->delete();
            DB::table('homegarden.product')->where($where)->delete();
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
}